package com.runa.monitor.platinterface.extend.nb.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @Author: fuhui
 * @Description:
 * @Date: 2021/7/27 13:09
 * @Version: 1.0
 */
public class MqttTemperaturePressureUtils {
    private static final Logger logger = LoggerFactory.getLogger(MqttTemperaturePressureUtils.class);

    private static String[] ctrlObjNames = new String[]{"阀门开度", "室内温度", "热表流速", "热表供回温差", "热表回温", "热表供温"};

    static String ctrlObjNames(Integer obj) {
        switch (obj) {
            case 1:
                return ctrlObjNames[0];
            case 2:
                return ctrlObjNames[1];
            case 3:
                return ctrlObjNames[2];
            case 4:
                return ctrlObjNames[3];
            case 5:
                return ctrlObjNames[4];
            case 6:
                return ctrlObjNames[5];
            default:
        }
        return null;
    }

    public static Integer getKeyByName(String name) {
        for (int i = 0; i < ctrlObjNames.length; i++) {
            if (ctrlObjNames[i].equals(name)) {
                return i + 1;
            }
        }
        return null;
    }
/*    *//**
     *  设备校时时间
     *//*
    public static List<SendCmdParamVo> addCheckTimeCmd(List<SendCmdParamVo> sendCmdParamVoList, Date checkTime) {
        long l = System.currentTimeMillis() - checkTime.getTime();
        if (l > 1000 * 60 ){
            sendCmdParamVoList.add(new SendCmdParamVo("rtc:", UtilTime.getCurrentTime("yyyyMMddHHmmss")));
        }
        return sendCmdParamVoList;
    }

    *//**
     * 下发唤醒周期
     *//*
    public static List<SendCmdParamVo> addWakeUpCycleCmd(List<SendCmdParamVo> sendCmdParamVoList, Boolean sendFlag, Integer wakeUpCycle, Date wakeUpCycleAddTime) {
        if (null != sendFlag && sendFlag) {
            if (null != wakeUpCycle && MqttSetUtils.checkCanSendWakeUpCycle(wakeUpCycleAddTime)) {
                sendCmdParamVoList.add(new SendCmdParamVo("citv:", wakeUpCycle + "_" + wakeUpCycle));//采样周期  数据1：供暖季  数据2：非供暖季 单位minutes
                sendCmdParamVoList.add(new SendCmdParamVo("uitv:", wakeUpCycle + "_" + wakeUpCycle));//冻结周期  数据1：供暖季  数据2：非供暖季 单位minutes
                sendCmdParamVoList.add(new SendCmdParamVo("sitv:", wakeUpCycle + "_" + wakeUpCycle));//上报周期  数据1：供暖季  数据2：非供暖季 单位minutes
            }
        }
        return sendCmdParamVoList;
    }

    *//**
     * 下发唤醒周期
     *//*
    public static List<SendCmdParamVo> addTemperatureCorrectionCmd(List<SendCmdParamVo> sendCmdParamVoList, NbLotWakeUpVo nbLotWakeUpVo) {
        if (null != nbLotWakeUpVo.getTc() && MqttSetUtils.checkCanSendTc(nbLotWakeUpVo.getTcAddTime())) {
            Double tc = nbLotWakeUpVo.getTc() * 10;
            sendCmdParamVoList.add(new SendCmdParamVo("Tc:", tc + "_0"));//采样周期  数据1：供暖季  数据2：非供暖季 单位minutes
        }
        return sendCmdParamVoList;
    }

    *//**
     * 更新状态
     * @param sendCmdParamVoList
     * @param stateCode
     *//*
    public static List<SendCmdParamVo> addCheckStaModelCmd(List<SendCmdParamVo> sendCmdParamVoList, String stateCode) {
        // 转换为2进制，判断最高位是否为1
//        String codeBinaryString = new BigInteger(stateCode, 16).toString(2);// 转成2进制
//        if (codeBinaryString.length() < 32){
//            String supplement = "";
//            for (int i = 0; i < 32-codeBinaryString.length(); i++) {
//                supplement += "0";
//            }
//            codeBinaryString = supplement + codeBinaryString;
//        }
//        if (!"1".equals(codeBinaryString.substring(0, 1))){
//            String restultCodeBinaryString = "1" + codeBinaryString.substring(1, codeBinaryString.length());
//            sendCmdParamVoList.add(new SendCmdParamVo("sta:", new BigInteger(restultCodeBinaryString, 2).toString(16)));
//        }
        //产品id  15099534 这个地方是威海用的，威海的温压监测仪是2021年生产的，需要下发sta值
        //肖顺需求更改下发80007800 SCDD000426
        sendCmdParamVoList.add(new SendCmdParamVo("sta:", "80007800"));
        return sendCmdParamVoList;
    }

    *//**
     * 发送温压监测仪命令到电信平台
     *
     * @param sendCmdParamVoList 命令参数
     * @param sendCmdConfigVo    电信产品参数
     *//*
    public static void sendToDxTemperaturePressureCmd(List<SendCmdParamVo> sendCmdParamVoList, SendCmdConfigVo sendCmdConfigVo) {
        if (!CollectionUtils.isEmpty(sendCmdParamVoList)) {
            String sendContent = MqttSetUtils.getSendCmdByParamList(sendCmdParamVoList);
            MqttSetUtils.sendDownCommand(sendCmdConfigVo, StringUtils.isNotEmpty(sendContent) ? sendContent + "," : "");
        }else{
            logger.info("设备"+sendCmdConfigVo.getDeviceId()+"无命令下发");
        }
    }*/


    /**
     * 默认00004000，00000000000000000100000000000000，[bit 14]R/W 1：T1通道开启                0：关闭
     * 状态码对应32种异常状态：
     * [bit 31]R/W 1：出厂                      0：未出厂
     * [bit 30]R   1：电压低于阈值              0：正常
     * [bit 29]R   1：T1超上下限                0：正常
     * [bit 28]R   1：T1周期阈值超限            0：正常
     * [bit 27]R   1：T2超上下限                0：正常
     * [bit 26]R   1：T2周期阈值超限            0：正常
     * [bit 25]R   1：P1超上下限                0：正常
     * [bit 24]R   1：P1周期阈值超限            0：正常
     * [bit 23]R   1：P2超上下限                0：正常
     * [bit 22]R   1：P2周期阈值超限            0：正常
     * [bit 21]R   1：NB上报失败                0：正常
     * [bit 20]R   1：NB模块初始化异常          0：正常
     * [bit 19]R   1：BLE模块初始化异常         0：正常
     * [bit 18]R   1：T1接入异常                0：正常
     * [bit 17]R   1：T2接入异常                0：正常
     * [bit 16]R   1：P1接入异常                0：正常
     * [bit 15]R   1：P2接入异常                0：正常
     * [bit 14]R/W 1：T1通道开启                0：关闭
     * [bit 13]R/W 1：T2通道开启                0：关闭
     * [bit 12]R/W 1：P1通道开启                0：关闭
     * [bit 11]R/W 1：P2通道开启                0：关闭
     * [bit 10]R/W 1：LCD显示开启               0：关闭
     * bit[9:0]保留：
     * 返回空值，表示正常，返回不为null，为故障信息
     */
    public static String getFaultMsgByCode(String code) {
        String codeFaultMsg = "";
        String codeBinaryString = Long.toBinaryString(Long.parseLong(code,16));// 转成2进制
        String[] codeBinaryArr = codeBinaryString.split("");
        for (int i = 0; i < codeBinaryArr.length; i++) {
            if (codeBinaryArr[i] == "1"){
                switch (i){
//                    case 10: codeFaultMsg = "LCD显示开启"; break;
//                    case 11: codeFaultMsg = "P2通道开启"; break;
//                    case 12: codeFaultMsg = "P1通道开启"; break;
//                    case 13: codeFaultMsg = "T2通道开启"; break;
//                    case 14: codeFaultMsg = "T1通道开启"; break;
                    case 15: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "P2接入异常" : "," + "P2接入异常"; break;
                    case 16: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "P1接入异常" : "," + "P1接入异常"; break;
                    case 17: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "T2接入异常" : "," + "T2接入异常"; break;
                    case 18: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "T1接入异常" : "," + "T1接入异常"; break;
                    case 19: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "BLE模块初始化异常" : "," + "BLE模块初始化异常"; break;
                    case 20: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "NB模块初始化异常" : "," + "NB模块初始化异常"; break;
                    case 21: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "NB上报失败" : "," + "NB上报失败"; break;
                    case 22: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "P2周期阈值超限" : "," + "P2周期阈值超限"; break;
                    case 23: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "P2超上下限" : "," + "P2超上下限"; break;
                    case 24: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "P1周期阈值超限" : "," + "P1周期阈值超限"; break;
                    case 25: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "P1超上下限" : "," + "P1超上下限"; break;
                    case 26: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "T2周期阈值超限" : "," + "T2周期阈值超限"; break;
                    case 27: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "T2超上下限" : "," + "T2超上下限"; break;
                    case 28: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "T1周期阈值超限" : "," + "T1周期阈值超限"; break;
                    case 29: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "T1超上下限" : "," + "T1超上下限"; break;
                    case 30: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "电压低于阈值" : "," + "电压低于阈值"; break;
//                    case 31: codeFaultMsg = StringUtils.isEmpty(codeFaultMsg) ? "出厂" : "," + "出厂"; break;
                    default : break; // bit[9:0]保留
                }
            }

        }
        return codeFaultMsg;
    }

    private static String getValue(String str) {
        if (StringUtils.isNotBlank(str)) {
            str = str.replace("[", "").replace("]", "");
        }
        return str;
    }


    public static String getValue(Map<String, String> paramMap, String... keys) {
        for (String key : keys) {
            String value = getValue(paramMap, key);
            if (null != value) {
                return getValue(value);
            }
        }
        return null;
    }

    public static String getValue(Map<String, String> paramMap, String key) {
        String value = paramMap.get(key);
        if (null != value) {
            return getValue(value);
        }
        return getValue(paramMap.get(key.toLowerCase()));
    }

    public static String[] getArrayValue(Map<String, String> paramMap, String key, String splitRegex) {
        String value = getValue(paramMap, key);
        if (null == value) {
            return new String[]{};
        }
        return value.split(splitRegex);
    }

    public static String filterStr(String str) {
        if (StringUtils.isNotBlank(str)) {
            str = str.replace("[", "").replace("]", "");
        }
        return str;
    }


}
